package com.yang.dao;

import com.yang.entity.Address;

import java.sql.*;
import java.util.*;

public class AddressDao {
    //查询全部
    public List<Address> queryAddress() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Address");
            rs = ps.executeQuery();
            List<Address> list = new ArrayList<>();
            while (rs.next()) {
                Address obj = new Address();
                obj.setId(rs.getInt("id"));
                obj.setContacts(rs.getString("contacts"));
                obj.setAddress(rs.getString("address"));
                obj.setTel(rs.getString("tel"));
                obj.setUserid(rs.getInt("userid"));
                obj.setDef(rs.getInt("def"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Address queryAddressById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Address where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Address obj = new Address();
                obj.setId(rs.getInt("id"));
                obj.setContacts(rs.getString("contacts"));
                obj.setAddress(rs.getString("address"));
                obj.setTel(rs.getString("tel"));
                obj.setUserid(rs.getInt("userid"));
                obj.setDef(rs.getInt("def"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveAddress(Address obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into address(contacts, address, tel, userId, def) values (?, ?, ?, ?, ?)");
            ps.setString(1, obj.getContacts());
            ps.setString(2, obj.getAddress());
            ps.setString(3, obj.getTel());
            ps.setInt(4, obj.getUserid());
            ps.setInt(5, obj.getDef());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteAddressById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Address where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateAddressById(Address obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update address set contacts=?, address=?, tel=?, userId=?, def=? where id=?");
            ps.setString(1, obj.getContacts());
            ps.setString(2, obj.getAddress());
            ps.setString(3, obj.getTel());
            ps.setInt(4, obj.getUserid());
            ps.setInt(5, obj.getDef());
            ps.setInt(6, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
